package com.whg.demo.biz;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @Author whg
 * @create 2022/11/18 10:56
 * @description：redis操作工具类
 */
@Component
public class RedisClient {

    private static final Logger logger = LoggerFactory.getLogger(RedisClient.class);

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Value("${spring.redis.expire.time}")
    private int expireTime;

    /**
     * 添加集合键值对
     * @param key
     * @param value
     */
    public Boolean set(String key, String value){
        try{
            redisTemplate.opsForValue().set(key,value,expireTime, TimeUnit.SECONDS);
            return true;
        }catch (Exception e){
            logger.error("添加集合键值对出错",e);
            return false;
        }
    }

    /**
     * 通过key值获取缓存中的值
     * @param key
     * @return
     */
    public String get(String key){
        String value = redisTemplate.opsForValue().get(key);
        return value;
    }

    /**
     * 刷新key的过期时间
     * @param key
     * @return
     */
    public Boolean setExpireTime(String key){
        return setExpireTime(key,expireTime);
    }

    /**
     * 设置key的过期时间
     * @param key
     * @return
     */
    public Boolean setExpireTime(String key,long seconds){
        try{
            redisTemplate.expire(key,seconds,TimeUnit.SECONDS);
            return true;
        }catch (Exception e){
            logger.error("设置key的过期时间出错",e);
            return false;
        }
    }

    public void setList(String key, List list){
        redisTemplate.opsForList().rightPushAll(key,list);
        redisTemplate.expire(key,expireTime, TimeUnit.SECONDS);
    }

    public List getList(String key){
        return redisTemplate.opsForList().range(key,0,-1);
    }

    public void append(String key,String value){
        redisTemplate.opsForList().rightPush(key,value);
        redisTemplate.expire(key,expireTime, TimeUnit.SECONDS);
    }

    public void delete(String key,String value){
        redisTemplate.opsForList().remove(key,0,value);
    }

    public void setSet(String key, Set<String> list){
        redisTemplate.opsForSet().add(key,list.toArray(new String[0]));
        redisTemplate.expire(key,expireTime, TimeUnit.SECONDS);
    }

    public Set getSet(String key){
        Set<String> set = redisTemplate.opsForSet().members(key);
        return set;
    }

    public void appendSet(String key,String value){
        redisTemplate.opsForSet().add(key,value);
        redisTemplate.expire(key,expireTime, TimeUnit.SECONDS);
    }

    public void deleteSet(String key,String value){
        redisTemplate.opsForSet().remove(key,value);
    }



}
